package com.ujiuye;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Set;

@Component
public class ClearImgJob {
    @Resource
    private RedisTemplate<String,String> template;
    @Value("${imgpath}")
    private String imgPath;

    @Scheduled(cron = "0 47 13 ? * *")
    public void job(){
        //获取Redisset集合
        SetOperations<String,String> opsForSet = template.opsForSet();
        //对俩个set集合进行求差集
        Set<String> difference = opsForSet.difference("SETMEAL_PIC_UPLOAD", "SETMEAL_PIC_DB");

        if (difference!=null && difference.size()>0){
            for (String pic : difference) {
                File file = new File(imgPath+pic);
                file.delete();
            }
        }
        template.delete("SETMEAL_PIC_UPLOAD");
        template.delete("SETMEAL_PIC_DB");
        System.out.println("正在清理图片");
        System.out.println(imgPath);
    }
}
